const YBKEY = 'YBEI-LOGIN'
const YB_USER_PHONE = 'YB-USER-PHONE'

//获取url中的参数
function getUrlParam(pname) {
    var reg = new RegExp("(^|&)" + pname + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}

//读取cookies
function getCookie(name)
{
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}
 
function jsCallSetLaravelSession() {
    var laravel_session = getCookie('laravel_session');
    var dict = {"laravel_session":laravel_session};
    console.log(dict);
    try{
        window.webkit.messageHandlers.jsCallSetLaravelSession.postMessage(dict);
    }catch(e){
        window.jspay.jsCallSetLaravelSessionAndroid(laravel_session);
    }
}

function jsCallSetLaravelSessionAndroid() {
    var laravel_session = getCookie('laravel_session');
    window.jspay.jsCallSetLaravelSessionAndroid(laravel_session);
}

function jsCallStartBizFaceCheckAndroid() {
    var laravel_session = getCookie('laravel_session');
    window.jspay.jsCallStartBizFaceCheckAndroid(laravel_session);
}

//调用APP实名认证，友盾IOS SDK
function jsCallVerifyCertification(phone) {
    var dict = {"phone":phone};
    console.log(dict);
    try{
        window.webkit.messageHandlers.jsCallLiving.postMessage(dict);
    }catch(e){
        window.jspay.jsCallVerifyCertificationAndroid(phone);
    }
}
//调用APP实名认证，友盾Android SDK
function jsCallVerifyCertificationAndroid(uid) {
    console.log(uid, 'Android');
    window.jspay.jsCallVerifyCertificationAndroid(uid);
}

function loginValidate() {
    axios.post('/api/user/islogined', {

    }).then(function (response) {
        var obj = response.data;
        if(obj.code != 0) {
            var backurl = self.location.href;
            location.href = 'login.html?backurl='+ backurl;
        } else {

        }
    }).catch(function (response) {
        console.log(response);
    });
}

function verifyCertification(platform) {
    var platform = getCookie('platform'); 
    var phone = getCookie(YB_USER_PHONE);
    if(platform == "android") {
        jsCallVerifyCertificationAndroid(phone); 
    } else {
        jsCallVerifyCertification(phone);
    }
}

function loginValidateGoto(  ) {
    axios.post('/api/user/islogined', {

    }).then(function (response) {
        var obj = response.data;
        if(obj.code == 0) {
            location.href = '/html/home.html';
        } else {
			
        }

    }).catch(function (response) {
        console.log(response);
    });
}

//主页登录验证，如果登录了，触发获取通讯录等信息
function homeLoginValidateGoto(platform) {
    axios.post('/api/user/islogined', {

    }).then(function (response) {
        var obj = response.data;
        if(obj.code != 0) {
            var backurl = self.location.href;
            location.href = 'login.html?act=logout&backurl='+ backurl;
        } else {
			//触发获取通讯录
            var platform = getCookie('platform');
			if(platform == "android") {
				jsCallSetLaravelSessionAndroid(); 
			} else {
				jsCallSetLaravelSession();
			}
		}
    }).catch(function (response) {
        console.log(response);
    });
}

window.reloadData = function() {
    location.reload();
}